import { Dictionary } from "./Dictionary";

/*
中文简体-ZH
中文繁体-ZHTW
荷兰语-NL
法语-FR
德语-DE
印尼语-IN
意大利语-IT
日语-JA
韩语-KO
马来语-MS
葡萄牙语-PT
西班牙语-ES
泰语-TH
土耳其语-TR
越南语-VI
阿拉伯语-AR
默认英语-EN
*/

export default class Localization 
{
    static  instance:Localization;

    static GetInstance()
    {
        if (Localization.instance == null)
        {
            Localization.instance = new Localization();
        }

        return Localization.instance;
    }

    constructor() {
        this.StartLanguan();
        this.LoadText();
    }
    
    static dic :Dictionary<string,string>= new Dictionary<string, string>();
    LoadText()
    {
        //let text:string=Laya.loader.getRes("Data/Language.txt");
        let text:string="";

        let allstr= this.SplitString(text, '\n');
        for (let i = 0; i < allstr.length; i++) {
            const element = allstr[i];
            if (element == null)
            {
                continue;
            }

            var IndexofA = element.indexOf('=');
            if (IndexofA == -1)
            {
                console.log("IndexofA="+IndexofA);
                continue;
            }

            try
            {
                Localization.dic.Add(element.substring(0, IndexofA), element.substring(IndexofA + 1, element.length - 1));
                
            }
            catch (error)
            {
                console.error("文字标识重复");
            }
        }

        console.log(Localization.dic);
    }

    public  SplitString(n:string, m:string){
        var list = n.split(m);
        return list;
    }

    static language="";
    StartLanguan()
    {
        let u= "EN";
        try {
            u= window["AndroidLanguage"]();
            u=u.toUpperCase();
            console.log("Language:"+u);
        } catch (error) {
            
        }

        //console.log("Language:"+"zHtw".toUpperCase());

        switch (u) {
            case "ZH":
            case "ZHTW":
            case "NL":
            case "FR":
            case "DE":
                case "IN":
                    case "IT":
                        case "JA":
                            case "KO":
                                case "MS":
                                    case "PT":
                                        case "ES":
                                            case "TH":
                                                case "TR":
                                                    case "VI":
                                                        case "AR":
                                                        case "EN":
                Localization.language=u+"_";
                break;
            default:
                Localization.language="EN"+"_";
                break;
        }
    }

    static GetValue( key:string)
    {
        if (Localization.language=="")
        {
            Localization.GetInstance();
        }

        if (Localization.dic.ContainsKey(Localization.language + key) == false)
        {
            return "";
        }
        
        let str=Localization.dic.TryGetValue((Localization.language + key))

        return str;
    }

}
